/**
 * 
 */
package com.res.system.referencedata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.res.common.controller.CatalogManagementPageController;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.system.utils.SimulatorUtilities;
import com.res.system.utils.SimulatorUtilities.ConfigData;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * 
 */
public class ReferenceDataManager {

    private static final Logger logger = Logger.getLogger(ReferenceDataManager.class);

    private List<Object[]> systemInfoList;

    private static List<Object> packageHierachyList;

    private static List<String> systemNameList;
    
    private static Map<String, Object> referenceDataMap = null;

    private static ReferenceDataManager manager;

    private ReferenceDataManager() throws Exception {

        load();
    }

    public void load() throws Exception {

        if (null == referenceDataMap || referenceDataMap.isEmpty()) {
            referenceDataMap = loadData();
        }

        systemInfoList = (List<Object[]>) referenceDataMap.get("SYSTEM");
        systemNameList = new ArrayList<String>();
        
        for (Object[] objectArray : systemInfoList) {

            referenceDataMap.put(String.valueOf(objectArray[0]), objectArray[1].toString());
            systemNameList.add(objectArray[1].toString());

        }

        packageHierachyList = (List<Object>) referenceDataMap.get("API");

        for (Object packageHierachy : packageHierachyList) {
            referenceDataMap.put((String) packageHierachy, packageHierachy.toString());
        }

    }

    /**
     * @param type
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private Map<String, Object> loadData() throws IOException, ClassNotFoundException {

        final CommunicationMessage message = new CommunicationMessage();

        final String serverAddress = SimulatorUtilities.getConfigData(ConfigData.SERVER_ADDRESS);
        final String port = SimulatorUtilities.getConfigData(ConfigData.SERVER_PORT);

        logger.info("Back End Call :" + "JOBType:GenericAPIExecution" + "|ID:" + serverAddress + "|Port:" + port + "|Executor:ReferenceDataLoad");

        message.setMessageHeader("JOBType:GenericAPIExecution|Executor:ReferenceDataLoad|ID:" + serverAddress + "|Port:" + port);

        final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
        final CommunicationMessage returnedMessage = plugin.invoke(message);

        final Map<String, Object> returnCollection = (Map<String, Object>) returnedMessage.getObject();

        return returnCollection;
    }
    
    
    
   /**
    *  
    */
    public static void cleanReferenceData(){
        
        referenceDataMap=null;
        systemNameList=null;
        packageHierachyList=null;
        
        
    }

    /**
     * @return
     * @throws Exception
     */
    public static ReferenceDataManager getInstance() throws Exception {

        if (null == manager || null == referenceDataMap || referenceDataMap.isEmpty()) {
            manager = new ReferenceDataManager();
        }

        return manager;
    }

    public List<Object[]> getSystemInfoList() {

        return systemInfoList;
    }

    public List<Object> getPackageHierachyList() {

        return packageHierachyList;
    }

    public List<String> getSystemNameList() {

        return systemNameList;
    }

}
